package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Sonic
 * @version : 1.0
 * @createTime : 2024-12-01  15:14
 * @description : 套餐表setmeal表管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api("SetMeal Api")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * @param: [setmealDTO]
     * @return: com.sky.result.Result
     * @description: 插入套餐
     **/
    @PostMapping
    @ApiOperation("insert setMeal")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result insert(@RequestBody SetmealDTO setmealDTO) {
        log.info("###### insert setMeal:{}", setmealDTO);
        setMealService.insert(setmealDTO);
        return Result.success();
    }

    /**
     * @description: 分页查询
     * 注意的点：1.Mapper查询的返回结果是 VO类型的，因为 setmeal中缺少 categoryName字段。
     * 2.Mapper.xml文件中左结合查询的 category中的 name字段需要起一个别名 categoryName，使之与 VO实体类中的字段名一致！！！
     **/
    @GetMapping("/page")
    @ApiOperation("paging query setMeal information")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("###### paging query setMeal information:{}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param: [ids]
     * @return: com.sky.result.Result
     * @description: 批量删除套餐信息和对应的套餐菜品表信息
     **/
    @DeleteMapping
    @ApiOperation("delete setMeal by ids")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("###### delete setMeal by ids:{}", ids);
        setMealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * @param: [id]
     * @return: com.sky.result.Result<com.sky.vo.SetmealVO>
     * @description: 根据id查询setmeal信息，用于修改时回显数据
     **/
    @GetMapping("/{id}")
    @ApiOperation("query setMeal by id")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("###### query setMeal by id");
        SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }
    /**
     * @param: [setmealDTO]
     * @return: com.sky.result.Result
     * @description: 更新setmeal
     **/
    @PutMapping
    @ApiOperation("update setmeal")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("###### update setmeal {}", setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * @param: [status, id]
     * @return: Result
     * @description: 根据id修改套餐销售状态
     **/
    @PostMapping("/status/{status}")
    @ApiOperation("change status")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result changeStatus(@PathVariable Integer status, Long id){
        setMealService.changeStatus(status, id);
        return Result.success();
    }
}
